﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DTO;

namespace DAO
{
    public class PhimDAO
    {
        public static DataClasses1DataContext data = new DataClasses1DataContext();

        //Select * From
        public static List<Phim> LayDSPhim()
        {
            try
            {
                var query = from phim in data.Phims select phim;
                List<Phim> list = new List<Phim>();
                foreach (var ph in query)
                {
                    list.Add(ph);
                }
                return list;

            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        //Insert into
        public static bool ThemPhim(Phim ph)
        {
            try
            {
                data.Phims.InsertOnSubmit(ph);
                data.SubmitChanges();
                return true;
            }
            catch
            {
                return false;
            }
        }

        //Update Where
        public static bool CapNhatPhim(Phim m_ph)
        {
            try
            {
                Phim ph = data.Phims.Single(phim => phim.ID == m_ph.ID);
                ph.TieuDe = m_ph.TieuDe;
                ph.NgayNhap = m_ph.NgayNhap;
                ph.DonGia = m_ph.DonGia;
                ph.SoLuong = m_ph.SoLuong;
                ph.ConLai = m_ph.ConLai;
                data.SubmitChanges();
                return true;
            }
            catch
            {
                return false;
            }
        }


        //Delete From Where
        public static bool XoaPhim(int ma)
        {
            try
            {
                Phim ph = data.Phims.Single(phim => phim.ID == ma);
                data.Phims.DeleteOnSubmit(ph);
                data.SubmitChanges();
                return true;
            }
            catch
            {
                return false;
            }
        }

        public static List<Phim> TimKiemMaPhim(string stringSearch)
        {
            try
            {
                var query = from phim in data.Phims where phim.ID.ToString().Equals(stringSearch) select phim;
                List<Phim> ResultSearch = new List<Phim>();
                foreach (Phim ph in query)
                {
                    ResultSearch.Add(ph);
                }
                return ResultSearch;
            }
            catch
            {
                return null;
            }
        }

        public static List<Phim> TimKiemNgayNhap(string stringSearch)
        {
            try
            {
                var query = from phim in data.Phims select phim;
                List<Phim> ResultSearch = new List<Phim>();
                foreach (Phim ph in query)
                {
                    if (ph.NgayNhap.ToString().Contains(stringSearch) || ph.NgayNhap.ToString().ToLower().Contains(stringSearch))
                        ResultSearch.Add(ph);
                }
                return ResultSearch;
            }
            catch
            {
                return null;
            }
        }

        public static bool CapNhatTinhTrangThue(int ma)
        {
            try
            {
                Phim ph = data.Phims.Single(phim => phim.ID == ma);
                ph.ConLai--;
                data.SubmitChanges();
                return true;
            }
            catch
            {
                return false;
            }
        }

        public static bool CapNhatTinhTrangTra(int ma)
        {
            try
            {
                Phim ph = data.Phims.Single(phim => phim.ID == ma);
                ph.ConLai++;
                data.SubmitChanges();
                return true;
            }
            catch
            {
                return false;
            }
        }
    }
}
